﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 图层字段
    /// </summary>
    public partial class CtlLayerField : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlLayerField()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmFieldsSetting_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 要显示字段的图层
        /// </summary>
        private IFeatureLayer _BindLayer = null;

        /// <summary>
        /// 创建图层字段控件
        /// </summary>
        /// <param name="featlyr">要显示字段的图层</param>
        public void Create(IFeatureLayer featlyr)
        {
            _BindLayer = featlyr;
            if (_BindLayer == null || _BindLayer.FeatureClass == null) return;
            IFeatureClass pFeatureClass = _BindLayer.FeatureClass;
            ILayerFields m_LayerFields = _BindLayer as ILayerFields;
            dgrdvwFields.Columns[2].ReadOnly = false;
            dgrdvwFields.Rows.Clear();
            dgrdvwFields.Rows.Add(m_LayerFields.FieldCount);
            for (int i = 0; i < m_LayerFields.FieldCount; i++)
            {
                IFieldInfo pFieldInfo = m_LayerFields.get_FieldInfo(i);
                IField pField = m_LayerFields.get_Field(i);
                dgrdvwFields.Rows[i].Cells[0].Value = pFieldInfo.Visible;
                dgrdvwFields.Rows[i].Cells[1].Value = pField.Name;
                dgrdvwFields.Rows[i].Cells[2].Value = pFieldInfo.Alias;
                dgrdvwFields.Rows[i].Cells[3].Value = pField.Type;
                dgrdvwFields.Rows[i].Cells[4].Value = pField.Length;
            }
        }

        /// <summary>
        /// 更新字段的状态：显示或别名
        /// </summary>
        public void UpdateStatus()
        {
            if (_BindLayer == null || _BindLayer.FeatureClass == null) return;
            ILayerFields m_LayerFields = _BindLayer as ILayerFields;
            if (m_LayerFields != null && this.dgrdvwFields.Rows.Count > 0)
            {
                for (int i = 0; i < m_LayerFields.FieldCount; i++)
                {
                    //算法更新，不需要遍历
                    IFieldInfo pFieldInfo = m_LayerFields.get_FieldInfo(i);
                    pFieldInfo.Visible = bool.Parse(this.dgrdvwFields.Rows[i].Cells[0].Value.ToString());
                    pFieldInfo.Alias = this.dgrdvwFields.Rows[i].Cells[2].Value.ToString().Trim();
                }
            }
        }
        
    }
}
